﻿using MY.ShoppingBackstage.BLL;
using MY.ShoppingBackstage.Model;
using MY.ShoppingBackstage.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MY.ShoppingBackstage.Controllers
{
    /// <summary>
    /// 添加图片
    /// </summary>
    public class FileUploadController : Controller
    {
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UploadImage(HttpPostedFileBase file)
        {

            //先定义文件
            string fileName = Guid.NewGuid().ToString();
            //图片存放的路径
            string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Pic");
            //如果没有图片
            if (Request.Files.Count == 0)
            {
                return Json(new { Success = false, FilePath = "" });
            }
            //获取图片的扩展名
            string stuf = Path.GetExtension(file.FileName);

            //图片存放路径不存在则创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            //保存图片
            string path = Path.Combine(filePath, fileName + stuf);
            file.SaveAs(path);
            string ulr = "/Pic/" + fileName + stuf;
            //返回前端
            return Json(new { Success = true, Url =ulr });

        }


        /// <summary>
        /// 图片页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Pic_Manage()
        {
            return View();
        }
        OperateResult operate = new OperateResult();
        PicService picService = new PicService();
        Product_SpuService product_SpuService = new Product_SpuService();//商品


        /// <summary>
        /// 添加图片
        /// </summary>
        /// <returns></returns>
        public ActionResult Add_Pic(string id)
        {
            Pic pic = new Pic();
            if (!string.IsNullOrEmpty(id))
            {
                int _id = Convert.ToInt32(id);
                pic = picService.Query(b => b.Pic_Id == _id);
            }
            ViewBag.Pic = pic;
            ViewBag.product = product_SpuService.QueryList(p => p.Id != 0);
            return View();
        }
       
        
        /// <summary>
        /// 添加或修改品牌
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult picAddORUpdate(Pic pic)
        {
            pic.ModifiedTime = DateTime.Now;
            if (pic.Pic_Id == 0)
            {
                //添加
                operate.Success = picService.Add(pic);
                operate.Info = operate.Success ? "添加成功" : "添加失败";
            }
            else
            {
                //修改
                operate.Success = picService.Update(pic);
                operate.Info = operate.Success ? "修改成功" : "修改失败";
            }
            return Json(operate);
        }
       
        
        /// <summary>
        /// 显示图片数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult picQueryTable(int page, int limit)
        {
            int count = 0;
            List<Pic> list = picService.QueryPageList(page, limit, b => b.Pic_Id != 0, b => b.Pic_Id);
            PageListResult<Pic> plr = new PageListResult<Pic>();
            plr.code = 0;
            plr.msg = "";
            plr.data = list;
            plr.count = picService.GetCount(a=>true,out count);

            return Json(plr, JsonRequestBehavior.AllowGet);
        }
      
        
        /// <summary>
        /// 修改图片状态
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult picUpdateStatus(Pic pic)
        {
            Pic _pic = picService.Query(b => b.Pic_Id == pic.Pic_Id);
            _pic.Pic_Status = pic.Pic_Status;
            operate.Success = picService.Update(_pic);
            return Json(operate);
        }

        /// <summary>
        /// 根据id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult picDeleteId(int id)
        {
            operate.Success = picService.Delete(id);
            return Json(operate);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public JsonResult Deletepic(string pic)
        {
            List<Pic> list = JsonConvert.DeserializeObject<List<Pic>>(pic);

            operate.Success = picService.Delete(list);
            return Json(operate);
        }


    }
}